<template>
  <div style="width: 100%; height: 100%">
    <div class="ding">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item label="标题">
          <el-input v-model="params.title" placeholder="按标题查找"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon='el-icon-search' @click="getList(1)">查询</el-button>
          <el-button type="danger" icon='el-icon-plus' @click="add">增加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table class="biaodan"
      :data="rows"
      stripe
      style="width: 1100px">
      <el-table-column
        prop="img"
        label="图片"
        >
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        >
      </el-table-column>
      <el-table-column
        prop="url"
        label="跳转地址"
       >
      </el-table-column>
      <el-table-column
        prop="status"
        label="是否启用"
       >
        <template slot-scope="scope">
             <el-switch
                v-model="scope.row.status "
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="statusChange(scope.row)"
                :active-value="1"
                :inactive-value="0">
              </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        width="320"
        >
        <template slot-scope="scope">
          <el-button type="primary" size='mini'
            icon='el-icon-edit'
            style="margin-right: 10px;"
            @click="edit(scope.row)"
          >编辑</el-button>
          <el-popconfirm
            title="确认删除此数据？"
            @confirm="remove(scope.row)"
          >
            <el-button slot="reference"  type="danger" size='mini'
            icon='el-icon-delete'>删除</el-button>
          </el-popconfirm>
          
        </template>
      </el-table-column>
    </el-table>

    <div class="ye">
      <el-pagination
      @size-change="sizeChange"
      @current-change="getList"
      :current-page="params.pageNum"
      :page-sizes="[5,10,100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
     <el-dialog :title="form._id ? '修改':'新增'" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="图片" prop="img">
          <el-input type="text" v-model="form.img"></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input type="text" v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="跳转地址" prop="url">
          <el-input type="text" v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="type in types"
              :key="type.value"
              :label="type.label"
              :value="type.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "shouyeBanner",
  components:{},

  data() {
    return {
      params:{
        title:'',
        type:'banner',
        pageNum:1,
        pageSize:10
      },
      rows:[],
      total:0,
      dialogFormVisible:false,
      form:{
        _id:0,
        img:'',
        type:'banner',
        url:'',
        title:'',
      },
      rules:{
        img:[
           {required:true,message:'图片不能为空',trigger:'blur'}
        ]
      },
      types:[
        {label:'轮播图',value:'banner'},
        {label:'分类区',value:'category'},
        {label:'为你推荐',value:'recommend'}
      ]
    };
  },
  methods: {
    
    getList(pageNum){
      if(pageNum){
          this.params.pageNum =pageNum
      }
    
      this.$http({
          url:'/admin/index',
          params:this.params
        }).then(res => {
          this.total = res.total
          this.rows = res.rows
   })
    },
    statusChange(row){
      this.$http({
            url:'/admin/index',
            method:'PUT',
            data:{
              indexId:row._id,
              status:row.status
            }
          }).then(res => {
            if(!res.errorMsg){
              this.getList(1)
            }
          })
    },
    
    sizeChange(pageSize){
      this.params.pageSize = pageSize
      this.getList(1)
    },
    add(){
      this.form = {
        _id:0,
        img:'',
        type:'banner',
        url:'',
        title:'',  
      }
      this.dialogFormVisible = true
    },
    
    edit(row){
      this.form = row
      this.dialogFormVisible = true
    },
    
    remove(row){
      this.$http({
            url:'/admin/index',
            method:'DELETE',
            params:{indexId:row._id}
          }).then(res => {
            if(!res.errorMsg){
              this.getList(1)
            }
          })
    },
    submit(){
      this.$refs.form.validate(valid => {
        if(valid){
          this.form.indexId = this.form._id
          this.$http({
            url:'/admin/index',
            method:this.form.indexId ? 'PUT':'POST',
            data:this.form
          }).then(res => {
            if(!res.errorMsg){
              this.dialogFormVisible = false
              this.getList(this.form.indexId ? undefined : 1)
            }
          })
        }
      })  
    }
  },
  created() {
    this.getList()

  },
  mounted() {},
};
</script>

<style scoped>
.demo-form-inline{
  width: 1000px;
  margin-left: 30px;
  line-height: 40px;
  height: 40px;
  
}
  .biaodan{
    
    
    margin-left: 30px;
  }
  .ye{
    margin-left: 30px;
  }
</style>
